/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cf.dao.impl;

/**
 *
 * @author Soltecn2
 */
import com.cf.dao.TbCategoriaDao;


import com.cf.modelo.TbCategoria;
import com.cf.util.HibernateUtil;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class TbCategoriaDaoImpl implements TbCategoriaDao {

    @Autowired
    SessionFactory sessionFactory;

    @Override
    public void add(TbCategoria categoria) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.save(this);

    }

    @Override
    public void edit(TbCategoria categoria) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.update(this);
    }

    @Override
    public void delete(int categoriaId) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.delete(getCategoria(categoriaId));
    }

    @Override
    public TbCategoria getCategoria(int categoriaId) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        return (TbCategoria) session.get(TbCategoria.class, categoriaId);
    }

    @Override
    public List<TbCategoria> listCategoria() {

        List<TbCategoria> lst = null;

        try {


            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            String sql = "from TbCategoria";
            Query query = session.createQuery(sql);
            lst = query.list();


        } catch (Exception e) {
            e.getMessage();
        }
        return lst;

    }
}
